﻿namespace LeetCodeProject._0001_1000._001_100._71_80._080;

public class Solution {
    public int RemoveDuplicates(int[] nums) {
        if (nums == null || nums.Length == 0)
            return 0;
        if (nums.Length == 2)
            return nums.Length;
        var i = 0;
        var j = 1;
        var isSecond = false;
        while (j < nums.Length)
        {
            if (nums[i] != nums[j])
            {
                isSecond = false;
                if (i + 1 == j)
                {
                    i++;
                    j++;
                }
                else
                {
                    nums[++i] = nums[j++];
                }
            }
            else    //ni==nj
            {
                if (isSecond)
                    j++;
                else
                {
                    isSecond = true;
                    nums[++i] = nums[j++];
                }
            }
        }
        return i + 1;
    }
}